﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Nuane.Posterous;

namespace PostMedia
{
	public partial class MainForm : Form
	{
		private PosterousSession _client;

		private const int ProgressBarResolution = 10000;

		public MainForm()
		{
			InitializeComponent();
			InitSiteList();
		}

		private void InitSiteList()
		{
			cbSites.SuspendLayout();
			cbSites.Items.Clear();
			if (_client != null)
			{
				PosterousSiteCollection sites = _client.GetSites();
				foreach (PosterousSite site in sites)
				{
					cbSites.Items.Add(site);
					if (site.IsPrimary)
						cbSites.SelectedItem = site;
				}
			}
			else
			{
				cbSites.Items.Add("Default site");
				cbSites.SelectedIndex = 0;
			}
			cbSites.ResumeLayout();
		}

		private void _client_UploadProgress(object sender, PosterousUploadProgressEventArgs e)
		{
			long p = ProgressBarResolution * (long)e.AlreadyUploadedBytes / (long)e.TotalBytes;
			if (progressBar.Value != p)
			{
				progressBar.Value = (int)p;
				Application.DoEvents();
			}
		}

		private void btnPost_Click(object sender, EventArgs e)
		{
			Connect();
			btnPost.Enabled = false;

			PosterousNewPost newPost = new PosterousNewPost();
			newPost.Title = txtTitle.Text;
			newPost.Body = txtBody.Text;

			int siteId;
			PosterousSite site = cbSites.SelectedItem as PosterousSite;
			if (site != null)
				siteId = site.Id;
			else
				siteId = 0;

			foreach (ListViewItem item in listMedia.Items)
			{
				PosterousFile file = (PosterousFile)item.Tag;
				newPost.Files.Add(file);
			}

			progressBar.Maximum = ProgressBarResolution;
			try
			{
				PosterousPostInfo post = _client.PublishPost(siteId, newPost);
				progressBar.Value = ProgressBarResolution;
				MessageBox.Show(string.Format("Posted successfully as {0}", post.ShortUri));
				progressBar.Value = progressBar.Maximum;
				listMedia.Items.Clear();
			}
			catch (PosterousException x)
			{
				MessageBox.Show(x.ToString());
			}
			finally
			{
				progressBar.Value = progressBar.Maximum;
				listMedia.Items.Clear();
				btnAdd.Enabled = true;
				btnPost.Enabled = true;
			}
		}

		private void btnConnect_Click(object sender, EventArgs e)
		{
			Connect();
		}

		private void Connect()
		{
			if (_client == null)
			{
				Cursor = Cursors.WaitCursor;
				Enabled = false;
				try
				{
					_client = new PosterousSession(txtMail.Text, txtPassword.Text);
					_client.ApplicationName = "posterapi";
					_client.ApplicationUri = new Uri("http://nuane.com/posterapi/");
					_client.UploadProgress += new EventHandler<PosterousUploadProgressEventArgs>(_client_UploadProgress);
					InitSiteList();
					txtMail.ReadOnly = true;
					txtPassword.ReadOnly = true;
					btnConnect.Enabled = false;
				}
				catch (PosterousException x)
				{
					_client = null;
					MessageBox.Show(x.ToString());
				}
				finally
				{
					Enabled = true;
					Cursor = Cursors.Default;
				}
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (openFileDialog.ShowDialog() != DialogResult.OK)
				return;

			string path = openFileDialog.FileName;
			PosterousFile pf = new PosterousFile(path);

			ListViewItem item = new ListViewItem(Path.GetFileName(path));
			item.SubItems.Add(Path.GetExtension(path));
			item.SubItems.Add(pf.FileLength.ToString());
			item.Tag = pf;
			
			listMedia.Items.Add(item);
		}


	}
}
